/**
 * Created by Jie on 2017/2/3.
 */
var db = require('../../util/pool');
var _ = require('underscore');

var Desktop = function() {};

Desktop.prototype.menu = function (json,callback) {
    // var desktpData = {
    //     "menu":[
    //         {"app":["m101","m102","m103"],"name":"1","menuid":"m001"}
    //         ],
    //     "apps":{
    //         "m101":{
    //             "isicon":1,"appid":"101","icon":"&#xe613","name":"功能1","width":"","iconclass":"layui-icon","url":"/core/menu","height":"","target":"_blank"
    //         },
    //         "m102":{
    //             "isicon":1,"appid":"102","icon":"&#xe613","name":"功能2","width":"","iconclass":"layui-icon","url":"/core/menu","height":"","target":"_blank"
    //         },
    //         "m103":{
    //             "isicon":1,"appid":"103","icon":"&#xe613","name":"功能3","width":"","iconclass":"layui-icon","url":"/core/menu","height":"","target":"_blank"
    //         }
    //     }
    // };
    var desktopData = {
        menu:[],
        apps:{}
    }
    var where = '(select menuId from role_fk_menu where isDel=0 and roleId='+json.roleId+')'
    var sql = 'SELECT id appid,isicon,icon,`name`,width,height,iconclass,url,`target`,iconbg FROM menu where isDel=0 and id in '+where+' order by sort desc';

    db.query(sql,function (err,results) {
        if(!err){
            var ids = [];
            var apps ="";
            for(var i=0;i<results.length;i++){
                ids.push('m'+results[i].appid);
                var mName = 'm'+results[i].appid;
                var sub = {
                    isicon:1,
                    appid:results[i].appid,
                    icon:results[i].icon,
                    name:results[i].name,
                    width:"",
                    iconclass:"layui-icon",
                    url:results[i].url,
                    height:"",
                    target:"_blank",
                    iconbg:results[i].iconbg
                };
                apps += mName +':'+JSON.stringify(sub)+',';
            }
            apps = apps.substring(0,apps.length-1);
            var main = [{app:ids,name:1,menuid:"m001"}];
            desktopData.menu = JSON.stringify(main);
            desktopData.apps = '{'+apps+'}';

            callback(null,desktopData);
        }
    });
    //callback(false, desktopData);
}

module.exports = Desktop;